<template>
	<view class="manageDevice">
		<navBar :title="$t('equipment.management')"></navBar>
		<view class="item-wrap">
			<view class="item flex_s">
				<view class="item-left">{{$t('home.Equipment-model')}}</view>
				<view class="item-right">{{deviceInfo.modelName || '未指定型号'}}</view>
			</view>
			<view class="item flex_s">
				<view class="item-left">{{$t('home.SN-number')}}</view>
				<view class="item-right">{{deviceInfo.sn}}</view>
			</view>

			<view class="item flex_s" @click="handleUpdateName">
				<view class="item-left">{{$t('home.Equipment-name')}}</view>
				<view class="item-right flex_1">
					<view>{{deviceInfo.deviceName || deviceInfo.sn}}</view>
					<image src="/static/image/icon-arrow.png"></image>
				</view>
			</view>

			<!-- 	<view class="item flex_s" @click="config.path('/pagesA/device/wifiNet/wifiNet')" >
				<view class="item-left">WiFi连接</view>
				<view class="item-right flex_1">
					<view>未连接</view>
					<image src="/static/image/icon-arrow.png"></image>
				</view>
			</view> -->

			<view class="item flex_s" @click="goPath">
				<view class="item-left">{{$t('home.Wifi-connectivity')}}</view>
				<view class="item-right flex_1">
					<!-- <view>未连接</view> -->
					<image src="/static/image/icon-arrow.png"></image>
				</view>
			</view>

			<view class="item flex_s" @click="config.path('/pagesA/device/updatesDevice/updatesDevice?sn='+sn+'&deviceversion='+deviceversion
      +'&isUpdate='+isUpdate+
      '&autoUpdate='+autoUpdate)">
				<view class="item-left">{{$t('home.System-update')}}</view>
				<view class="item-right flex_1">
					<view></view>
					<image src="/static/image/icon-arrow.png"></image>
				</view>
			</view>

			<view class="item flex_s" @click="handleUnbind">
				<view class="item-left">{{$t('home.Device-unbinding')}}</view>
				<view class="item-right flex_1">
					<view></view>
					<image src="/static/image/icon-arrow.png"></image>
				</view>
			</view>

		</view>
		<PopupName ref="PopupName" :title="title" :value="parentValue" @confirm="inputValue"></PopupName>
		<PopupUnbind ref="PopupUnbind" @confirm="handleUnbindConfirm"></PopupUnbind>
	</view>
</template>

<script>
	import PopupName from '@/componets/PopupName.vue';
	import PopupUnbind from '@/componets/PopupUnbind.vue';
	import {
		deviceInfo,
		deviceRemoveBind,
		updateDeviceName
	} from '@/api/device.js'
	export default {
		components: {
			PopupName,
			PopupUnbind
		},
		data() {
			return {
				title: '修改设备名称',
				deviceInfo: {}, //设备信息
				parentValue: '', // 设备名称，用于回显
				sn: '',
				deviceversion: '',
				isUpdate: '',
				autoUpdate: ""
			};
		},
		onLoad(options) {

			this.sn = options.sn;
			this.getDeviceInfo();
		},
		onShow() {},
		methods: {
			goPath() {
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
					}
				});
				const that = this
				that.config.path('/pagesA/device/wifiNet/search')


			},
			// 获取设备列表
			getDeviceInfo() {
				deviceInfo({
					sn: this.sn
				}).then((r) => {
					this.deviceInfo = r.data;
					this.deviceversion = r.data.deviceVersion
					this.isUpdate = r.data.isUpdate
					this.autoUpdate = r.data.autoUpdate
					this.parentValue = this.deviceInfo.deviceName
				});
			},
			// 修改设备名弹窗
			handleUpdateName() {
				this.parentValue = this.deviceInfo.deviceName || this.deviceInfo.sn;
				this.$refs.PopupName.open();
			},
			// 修改设备名称
			inputValue(newValue) {
				updateDeviceName({
					sn: this.sn,
					deviceName: newValue
				}).then((r) => {
					if (r.code === 0) {
						this.config.Toast(this.$t('modified.successfully'))
						this.deviceInfo.deviceName = newValue;
						this.$forceUpdate();
					}
				});
			},
			// 设备解绑
			handleUnbind() {
				this.$refs.PopupUnbind.open();
			},
			// 解绑确认
			handleUnbindConfirm() {
				deviceRemoveBind({
					sn: this.sn
				}).then((r) => {
					if (r.code === 0) {
						this.config.Toast(this.$t('unbind.successfully'))
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/device/device'
							})
						}, 1000)
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.manageDevice {
		.item-wrap {
			.item {
				border-bottom: 1px solid #f5f5f5;
				padding: 25rpx 40rpx;
				background: #fff;
				color: #333333;
				font-size: 32rpx;

				.item-right {
					image {
						width: 13rpx;
						height: 22rpx;
						margin-left: 10rpx;
					}
				}
			}
		}
	}
</style>